﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * POP3 Response Class
 *      Represents and manages a general POP3 protocol response to a command.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using Adaptive.Foundation;
using Adaptive.Foundation.Network.Cnr;

namespace Adaptive.Foundation.Network.Pop3
{
    /// <summary>
    /// Represents and manages a general POP3 protocol response to a command.
    /// </summary>
    public class Pop3Response : CnrResponseBase
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Constants Declarations
         *------------------------------------------------------------------------------*/
        #region Private Constants Declarations
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations

        private CnrErrorCode<string> _responseCodeInstance;
        private Pop3Parser _parser;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public Pop3Response()
            : base(string.Empty)
        {
            _responseCodeInstance = new PositiveResponseCode();
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="responseText">
        /// The text that defines the response content.
        /// </param>
        public Pop3Response(string responseText)
            : base(responseText)
        {
            ParseContent(responseText);
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_responseCodeInstance != null)
                    _responseCodeInstance.Dispose();
            }

            _responseCodeInstance = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods

        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            base.Clear();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override string RenderResponse()
        {
            return RenderResponse(new Pop3Renderer());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="renderer"></param>
        /// <returns></returns>
        protected override string RenderResponse(CnrDataRendererBase renderer)
        {
            Pop3Renderer popRenderer = (Pop3Renderer)renderer;

            return popRenderer.RenderErrorCode(_responseCodeInstance);

        }
        /// <summary>
        /// Parses the supplied text.
        /// </summary>
        /// <param name="parser">A POP3 Parser instance used to parse the server text.</param>
        /// <param name="originalData">The original text from the remote host to be parsed.</param>
        protected override void ParseResponse(CnrDataParserBase parser, string originalData)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override CnrDataParserBase CreateParserInstance()
        {
            return new Pop3Parser();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override CnrDataRendererBase CreateRendererInstance()
        {
            return new Pop3Renderer();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions

        /// <summary>
        /// Parses the content.
        /// </summary>
        /// <param name="originalContent">Content of the original.</param>
        protected override void ParseContent(string originalContent)
        {
            
            string[] data = null;

            _parser = new Pop3Parser();
            data = _parser.ParseProtocolDataIntoParts(originalContent);

            if (data[0].ToLower() == "+ok")
            {
                _responseCodeInstance = new PositiveResponseCode();
                Success = true;
            }
            else
            {
                Success = false;
                _responseCodeInstance = new NegativeResponseCode();
            }

            base.ResponseData = data[1];

        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
